import React, { Component } from "react";

import { Link, Route, Switch } from "react-router-dom";

class Home extends Component {
  render() {
    return <div>这是home页面</div>;
  }
}
class About extends Component {
  render() {
    return <div>这是about页面</div>;
  }
}
class Detail extends Component {
  // 如果当前组件作为了路由页面，那么它就会有props对象，就是路由信息
  // this.props.match.params.id可以获取动态路由参数
  // ?.表示可选链操作符
  render() {
    console.log(this);
    return <div>这是详情页面 - {this.props.match?.params?.id}</div>;
  }
  componentDidMount() {}
}

class App extends Component {
  render() {
    return (
      <>
        <h2>react-router-v5-动态路由传参</h2>
        <ul>
          <li>
            <Link to="/home">首页</Link>
          </li>
          <li>
            <Link to="/about">关于页</Link>
          </li>
          <li>
            <Link to="/detail/234">详情页</Link>
          </li>
          <li>
            <Link to="/detail/789">详情页2</Link>
          </li>
        </ul>

        <Switch>
          <Route path="/home" component={Home}></Route>
          <Route path="/about" component={About}></Route>
          {/* 动态路由传参和vue一样，/:id */}
          <Route path="/detail/:id" component={Detail}></Route>
        </Switch>
      </>
    );
  }
}

export default App;
